<template>
  <el-table
    :data="tableData"
    border
    max-height="540"
    style="width: 100%">
    <el-table-column
      prop="c_no"
      label="课程编号">
    </el-table-column>
    <el-table-column
      prop="c_title"
      label="课程名称">
    </el-table-column>
    <el-table-column
      prop="t_no"
      label="教师编号">
    </el-table-column>
    <el-table-column
      prop="t_name"
      label="教师姓名">
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作">
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row)" type="text" size="small">选课</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import { sccourseAPI, choosecourseAPI } from '@/api/studentAPI.js'

export default {
  name: 'Score',
  data () {
    return {
      tableData: [],
      no: ''
    }
  },
  created () {
    this.no = JSON.parse(sessionStorage.getItem('token')).username
    this.getTableData()
  },
  methods: {
    // 选课
    handleClick (row) {
      this.$confirm(`是否选修课程《${row.c_title}》?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async () => {
        const params = {
          sno: this.no,
          cno: row.c_no,
          tno: row.t_no
        }
        const { data: res } = await choosecourseAPI(params)
        if (res.status !== 200) {
          this.$message.error(res.data.msg)
        } else {
          this.$message.success(res.data.msg)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消选课'
        })
      })
    },
    // 获取表格数据
    async getTableData () {
      const { data: res } = await sccourseAPI()
      if (res.status !== 200) return this.$message.error(res.data.msg)
      this.tableData = res.data
    }
  }
}
</script>

<style>

</style>
